# -*- coding: utf-8 -*-

import jw.svm_model as svm_model
from py2neo import Graph,Node,Relationship
from jw.svm_model import test
import fool

test_graph = Graph(
    "http://localhost:7474",
    port= 7474,
    username="neo4j",
    password="456852"
)


def q_type_0(entities):
    res = []
    for i in entities[1]:
        cypher = """match(s:D)-[r:located_at]->(t:B) where s.name='{}' return t """.format(i)
        temp = test_graph.run(cypher).data()
        res.extend(temp)
    answer = ""
    for x in res:
        answer += str(x['t']['name']) + ","
    #   print(x['g']['name'])
    return answer + '\n'

def q_type_1(entities):
    res = []
    for i in entities[2]:
        cypher = """match(s:D)-[r:has_symptom]->(t:S) where t.name='{}' return s """.format(i)
        temp = test_graph.run(cypher).data()
        res.extend(temp)
    answer = ""
    for x in res:
        answer += str(x['s']['name']) + ","
    #   print(x['g']['name'])
    return answer + '\n'

def q_type_2(entities):
    res = []
    for i in entities[2]:
        cypher1 = """match(s:D)-[r:has_symptom]->(t:S) where t.name='{}' return s """.format(i)
        temp1 = test_graph.run(cypher1).data()
        for i in temp1:
            cypher = """match(d:D)-[r:need_check]->(c:C) where d.name='{}' return c""".format(i['s']['name'])
            temp2 = test_graph.run(cypher).data()
            res.extend(temp2)
    answer = ""
    for x in res:
        answer += str(x['c']['name']) + ","
    #   print(x['g']['name'])
    return answer + '\n'


def q_type_3(entities):
    res = []
    print(3)
    for i in entities[0]:
        cypher = """match(s:D)-[r:located_at]->(t:B) where t.name='{}' return s """.format(i)
        temp = test_graph.run(cypher).data()
        res.extend(temp)
    answer = ""
    for x in res:
        answer += str(x['s']['name']) + ","
    #   print(x['g']['name'])
    return answer + '\n'


def get_query_type(query):

    q_type, entities = test(query)
    try:
        answer = solution_dict[q_type](entities)
    except:
        answer = "Sorry...I don't know this answer TAT ..."
    return entities, answer, q_type


solution_dict = {0: q_type_0, 1:q_type_1,2:q_type_2, 3:q_type_3}

if __name__=="__main__":
    while(1):
        try:
            get_query_type()
        except:
            get_query_type()
            #print("~~~Bing: 5555555~")